﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MJM.RPP.EntidadesNegocio;
using System.Data.SqlClient;
using System.Data;

namespace MJM.RPP.AccesoDatos
{
    public class AreaAD
    {
        public List<AreaEN> Listar()
        {
            List<AreaEN> lst = new List<AreaEN>();
            SqlConnection conn = new SqlConnection(Cadena.CadenaTramite());
            SqlCommand cmd = new SqlCommand("sp_s_WEB_M_MAES_OFIC_Lista", conn);
            SqlDataReader dr;
            AreaEN obj;
            try
            {
                cmd.Connection.Open();
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    obj = new AreaEN();
                    obj.Area = dr.GetString(1);
                    obj.Codigo = dr.GetInt32(0);
                    lst.Add(obj);
                }

                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch (Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
            }

            return lst;
        }

        public List<AreaEN> ListarParam(String Area)
        {
            List<AreaEN> lst = new List<AreaEN>();
            SqlConnection conn = new SqlConnection(Cadena.CadenaTramite());
            SqlCommand cmd = new SqlCommand("sp_s_WEB_M_MAES_OFIC_ListaV2", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader dr;
            AreaEN obj;
            SqlParameter prmArea;
            try
            {
                prmArea = new SqlParameter();
                prmArea.ParameterName = "@vOFIC_chDESOFI";
                prmArea.SqlDbType = SqlDbType.VarChar;
                prmArea.Value = Area;
                cmd.Parameters.Add(prmArea);

                cmd.Connection.Open();
                dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    obj = new AreaEN();
                    obj.Area = dr.GetString(1);
                    obj.Codigo = dr.GetInt32(0);
                    lst.Add(obj);
                }

                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch (Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
            }

            return lst;
        }
    }
}
